//
//  LCWUpdatePicCollectionView.swift
//  MyWB
//
//  Created by 李成万 on 16/7/3.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit

let reuseId = "LCWUpdatePicCollectionView"

class LCWUpdatePicCollectionView: UICollectionView {

    
    var imageArr : [UIImage] = [UIImage]()
    var updatePicCollectionViewClosure:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout:UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func addImage(image:UIImage){
        
        imageArr.insert(image, atIndex: 0)
        
        if imageArr.count > 9{
            imageArr.removeLast()
        }
        reloadData()
    }
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        dataSource = self
        delegate = self
        registerClass(LCWCollectionCell.self, forCellWithReuseIdentifier: reuseId)
        
        let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
        let WH = (SCREENW - 10 - 20)/3
        flowLayout.itemSize = CGSize(width: WH, height: WH)
        
        flowLayout.minimumInteritemSpacing = 10
        flowLayout.minimumLineSpacing = 10
        
    }

}

extension LCWUpdatePicCollectionView : UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if imageArr.count == 0 || imageArr.count == 9{
            return self.imageArr.count
            
        }else{
            return self.imageArr.count + 1
        }
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseId, forIndexPath: indexPath) as! LCWCollectionCell
        if imageArr.count == indexPath.item{
            cell.image = nil
        }else{
            cell.image = imageArr[indexPath.item]
        }
//        cell.backgroundColor = randomColor()
        //右上角删除按钮闭包实现
        cell.LCWCollectionCellClosure = {
            self.imageArr.removeAtIndex(indexPath.item)
            self.reloadData()
        }
        
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        if imageArr.count == indexPath.item{
            updatePicCollectionViewClosure?()
        }
    }
    
}


class LCWCollectionCell : UICollectionViewCell{
    
    var LCWCollectionCellClosure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var image : UIImage? {
        didSet{
            if image == nil{
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteBTn.hidden = true
            }else{
                
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                deleteBTn.hidden = false
            }
            
        }
    }
    
    private lazy var bgImageView : UIImageView = UIImageView()
    private lazy var deleteBTn : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    
    func setupUI(){
        addSubview(bgImageView)
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
//        bgImageView.backgroundColor = randomColor()
        addSubview(deleteBTn)
        deleteBTn.snp_makeConstraints { (make) -> Void in
            make.top.trailing.equalTo(contentView)
        }
        
        
    }
    //删除按钮点击方法
    func deleteBtnClick(){
        LCWCollectionCellClosure?()
    }
}